﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TechProg
{
  public class RunTuring//run
  {
    public char[] lenta;
    public char[] alphabet;
    public List<rules> OurRules = new List<rules>();
    public rules tmpRule;
    public bool FirstStep = true;
    public int Condition;
    public int Position;
    public int LentaLength;
    char symbol;

    public RunTuring()
    {
      lenta = new char[10000];
      for (int i = 0; i < 100; i++)
      {
        lenta[i] = '_';
      }
      alphabet = new char[6];
      Condition = 0;
      Position = 0;
      symbol = '\0';
      LentaLength = 3;
    }
    public void AddRule(char CurrentSymbol, char NewSymbol, int PrevCondition, int NextCondition, char Direction)
    {
      OurRules.Add(new rules(CurrentSymbol, NewSymbol, PrevCondition, NextCondition, Direction ));
    }

    public void DeleteRule(int number)
    {
      OurRules.Remove(OurRules[number]);
    }

    public rules FindRule(char symbol, int Condition)
    {
      rules tmpRule = new rules('\0', '\0', 0, 0, '\0');
      tmpRule.prevCondition = 100;
      for (int i = 0; i < OurRules.Count(); i++)
      {
        if (OurRules[i].currentSymbol == symbol && OurRules[i].prevCondition == Condition)
        {
          return OurRules[i];
        }
      }
      return tmpRule;
    }

    public int step()
    {
      if (FirstStep)
      {
        Condition = 1;
        Position = 0;
        symbol = lenta[Position];
        FirstStep = false;
      }
      symbol = lenta[Position];
      tmpRule = FindRule(symbol, Condition);
      if (tmpRule.prevCondition == 100)
        return 2;
      lenta[Position] = tmpRule.newSymbol;
      Condition = tmpRule.nextCondition;
      if (tmpRule.direction.Equals('R') || tmpRule.direction.Equals('r'))
      {
        Position++;
        if (Position > LentaLength)
        {
          LentaLength++;
          if (LentaLength == 100)
          {
            return 1;
          }
        }
      }
      else if (tmpRule.direction.Equals('L') || tmpRule.direction.Equals('l'))
      {
        Position--;
        if (Position == 0)
        {
          if (LentaRight() == 1)
            return 1;
          Position = 1;
        }
      }
      else
      {
        Condition = 0;
      }
      return 0;
    }

    public int LentaRight()
    {
      if (LentaLength == 100)
      {
        return 1;
      }
      for (int i = LentaLength; i >=0; i--)
      {
        lenta[i + 1] = lenta[i];
      }
      lenta[0] = '_';
      LentaLength++;
      if (LentaLength == 100)
      {
        return 1;
      }
      return 0;
    }

    public int CheckAlphabet(char symbol)
    {
      int state = 0;
      for (int i = 0; i < alphabet.Length; i++)
      {
        if (alphabet[i] == symbol)
          state = 1;
      }
      return state;
    }

  }
}
